//
//  DetailMovieViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailMovieViewController.h"
#import "DetailMovieView.h"
#import "LoginViewController.h"
#import "Movie.h"

@interface DetailMovieViewController ()


@end

@implementation DetailMovieViewController


- (void)dealloc
{
    [_mov release];
    [_detailMovie release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.mov.movieName;
    
//    self.detailMovie = [[Movie alloc] init];
    
    
    [self getData];
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(barAction:)];
    



}

- (void)getData
{

    /*************************详情界面信息*********************/
    NSString *string1 = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=";

    NSString *detailConnector = [string1 stringByAppendingString: self.mov.movieId];
    NSLog(@"%@", detailConnector);
    
    // detailConnector是对的,但是解析出来的东西是错的
    
    NSURL *detailUrl = [NSURL URLWithString:detailConnector];
    
    NSURLRequest *detailRequest = [NSURLRequest requestWithURL:detailUrl];
    
    NSURLSession *detailSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionDataTask *getDetailTask = [detailSession dataTaskWithRequest:detailRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        
        NSDictionary *detailDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *detailTempDic = [detailDic objectForKey:@"result"];
        NSLog(@"%@", detailTempDic);
        // 这里很可能错
        // 没获取到数据
        self.detailMovie = [[Movie alloc] init];
        [self.detailMovie setValuesForKeysWithDictionary:detailTempDic];
//        NSLog(@"%@", self.detailMovie);

//        self.detailMovie.actors = (NSString *)[detailTempDic objectForKey:@"actors"];
//            NSLog(@"%@", self.detailMovie.actors);
        
        DetailMovieView *detailV = [[DetailMovieView alloc] initWithFrame:self.view.frame];
        
        detailV.labelDate.text = self.detailMovie.release_date;
            detailV.labelTime.text = self.detailMovie.runtime;
            detailV.labelStyle.text = self.detailMovie.genres;
            detailV.labelCountry.text = self.detailMovie.film_locations;
        detailV.labelMakerContent.text = self.detailMovie.actors;
        detailV.labelPlotContent.text = self.detailMovie.plot_simple;
        detailV.imageV.image = self.detailMovie.image;
        
        
        [self.view addSubview:detailV];
        [detailV release];
        
    }];
    
    [getDetailTask resume];
    
    /*************************详情界面信息*********************/
    
    
}

- (void)barAction:(UIBarButtonItem *)sender
{
    if (sender == self.navigationItem.leftBarButtonItem) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        [loginVC release];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
